package GUI;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmSendMailGUI.java
 *
 * Created on May 19, 2009, 10:46:36 AM
 */
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import DTO.*;
import BUS.*;
import java.util.Date;
/**
 *
 * @author ldtung_it
 */
public class FrmSendMailGUI extends javax.swing.JFrame {

    /** Creates new form FrmSendMailGUI */
    public FrmSendMailGUI() {
        initComponents();
        /**
         * Show center screen
         */
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setLocation((screenWidth - this.getSize().width) / 2, (screenHeight - this.getSize().height) / 2);

        /**
         * Resize column
         */
        //TableColumn tc = this.tableReceiver.getColumnModel().getColumn(this.tableReceiver.getColumnCount()-1);
        this.tableReceiver.getColumnModel().getColumn(0).setResizable(true);
        this.tableReceiver.getColumnModel().getColumn(0).setMinWidth(0);
        this.tableReceiver.getColumnModel().getColumn(0).setMaxWidth(300);
        this.tableReceiver.getColumnModel().getColumn(0).setPreferredWidth(100);
        /**
         * Add contact to combobox Sender
         */
//        String nameSender = "Tung";
//        String mailSender = "ldtung.it@gmail.com";
//        String typeMail = "Gmail";
//        this.cbSender.addItem(nameSender + " <" + mailSender + "> " + typeMail);
        /**
         * Add combobox To,CC,BCC to first column of table receivers
         */
        JComboBox cbtypeSend = new JComboBox();
        cbtypeSend.addItem("To:");
        cbtypeSend.addItem("Cc:");
        cbtypeSend.addItem("Bcc:");

        this.tableReceiver.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(cbtypeSend));
        this.tableReceiver.setValueAt("To:", 0, 0);

         AccountMailBUS accBus = new AccountMailBUS();
        ArrayList<AccountMailDTO> lst;
        try {
            lst = accBus.getListAccount();
             for(AccountMailDTO mail : lst){
                this.cbSender.addItem(mail.getAccountName() + " <" + mail.getMailAddress()+"> " + mail.getOutgoingServerName());
                
        }

        } catch (SQLException ex) {
            Logger.getLogger(FrmSendMailGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    //pack();

    }
    public void setReceiver(String receiver){
        this.tableReceiver.setValueAt(receiver, 0, 1);
    }
    public void setSubject(String subject){
        this.tfSubject.setText(subject);

    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btSend = new javax.swing.JButton();
        btSave = new javax.swing.JButton();
        btAttach = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbSender = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableReceiver = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        tfSubject = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtAreaContent = new javax.swing.JTextArea();
        jPanel4 = new javax.swing.JPanel();
        lbFile1 = new javax.swing.JLabel();
        lbFile2 = new javax.swing.JLabel();
        lbFile4 = new javax.swing.JLabel();
        lbFile3 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        jToolBar1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(204, 0, 51), null, null));
        jToolBar1.setRollover(true);

        btSend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Send message.png"))); // NOI18N
        btSend.setText("Send");
        btSend.setToolTipText("Send mail");
        btSend.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btSend.setFocusable(false);
        btSend.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btSend.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSendActionPerformed(evt);
            }
        });
        jToolBar1.add(btSend);

        btSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/save.png"))); // NOI18N
        btSave.setText("Save");
        btSave.setToolTipText("Save mail");
        btSave.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btSave.setFocusable(false);
        btSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaveActionPerformed(evt);
            }
        });
        jToolBar1.add(btSave);

        btAttach.setIcon(new javax.swing.ImageIcon(getClass().getResource("/attach.png"))); // NOI18N
        btAttach.setText("Attach");
        btAttach.setToolTipText("Attach more file");
        btAttach.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btAttach.setFocusable(false);
        btAttach.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btAttach.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btAttach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAttachActionPerformed(evt);
            }
        });
        jToolBar1.add(btAttach);

        jLabel1.setText("From: ");

        tableReceiver.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "", ""
            }
        ));
        tableReceiver.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableReceiver.setSurrendersFocusOnKeystroke(true);
        jScrollPane1.setViewportView(tableReceiver);
        tableReceiver.getColumnModel().getColumn(0).setMinWidth(0);
        tableReceiver.getColumnModel().getColumn(0).setPreferredWidth(2);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbSender, 0, 618, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 663, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbSender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jLabel2.setText("Subject:");

        txtAreaContent.setColumns(20);
        txtAreaContent.setRows(5);
        jScrollPane2.setViewportView(txtAreaContent);

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbFile1.setForeground(new java.awt.Color(153, 153, 0));
        lbFile1.setText("...");

        lbFile2.setForeground(new java.awt.Color(153, 153, 0));
        lbFile2.setText("...");

        lbFile4.setForeground(new java.awt.Color(153, 153, 0));
        lbFile4.setText("...");

        lbFile3.setForeground(new java.awt.Color(153, 153, 0));
        lbFile3.setText("...");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbFile1)
                .addGap(24, 24, 24)
                .addComponent(lbFile2)
                .addGap(42, 42, 42)
                .addComponent(lbFile3)
                .addGap(40, 40, 40)
                .addComponent(lbFile4)
                .addContainerGap(226, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(lbFile2)
                .addComponent(lbFile3)
                .addComponent(lbFile4)
                .addComponent(lbFile1))
        );

        jLabel3.setText("Files attach: ");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 663, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfSubject, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jMenu1.setText("File");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem1.setText("Exit");
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 663, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Choose file to attach
     * @param evt
     */
    private void btAttachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAttachActionPerformed
        // TODO add your handling code here:
        JFileChooser fileChoose = new JFileChooser();
        fileChoose.setMultiSelectionEnabled(false);
        int result = fileChoose.showOpenDialog(this);
        if (result == fileChoose.OPEN_DIALOG) {
            //for(File file : fileChoose.getSelectedFiles()){
            //  if(file.isFile()){
            File file = fileChoose.getSelectedFile();
            if (file.isFile()) {
                if (this.lbFile1.getText() == "...") {
                    this.lbFile1.setText(file.getName());
                    this.lbFile1.setToolTipText(file.getPath());
                } else if (this.lbFile2.getText() == "...") {
                    this.lbFile2.setText(file.getName());
                    this.lbFile2.setToolTipText(file.getPath());
                } else if (this.lbFile3.getText() == "...") {
                    this.lbFile3.setText(file.getName());
                    this.lbFile3.setToolTipText(file.getPath());
                } else if (this.lbFile4.getText() == "...") {
                    this.lbFile4.setText(file.getName());
                    this.lbFile4.setToolTipText(file.getPath());
                }
            }

        }
}//GEN-LAST:event_btAttachActionPerformed
    

    

    

    /**
     * Get info to send mail to receiver
     * @param evt
     */
    private void btSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSendActionPerformed
        // TODO add your handling code here:
        /**
         * Get info
         */
        //mail sender
        String sender = this.cbSender.getSelectedItem().toString();
        AccountMailBUS bus = new AccountMailBUS();
        String pass ;   //temp
        String mailServer = "smtp.gmail.com";//temp

        int leftPos = sender.indexOf("<");
        int rightPos = sender.indexOf(">");
        sender = sender.substring(leftPos + 1, rightPos);
        AccountMailDTO mai1l = null;
        try {
            mai1l = bus.getMailFromAddress(sender);
        } catch (SQLException ex) {
            Logger.getLogger(FrmSendMailGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        if ( mai1l == null)
        {
            JOptionPane.showMessageDialog(null, "Incorrect Mail Account");
            return;
        }
        pass = mai1l.getPassword();
        mailServer = mai1l.getOutgoingServerName();
        //Subject
        String subject = this.tfSubject.getText();
        //File
        ArrayList<File> fileAttach = new ArrayList();
        File file;
        if (this.lbFile1.getText() != "...") {
            file = new File(this.lbFile1.getToolTipText());
            fileAttach.add(file);
        }
        if (this.lbFile2.getText() != "...") {
            file = new File(this.lbFile2.getToolTipText());
            fileAttach.add(file);
        }
        if (this.lbFile3.getText() != "...") {
            file = new File(this.lbFile3.getToolTipText());
            fileAttach.add(file);
        }
        if (this.lbFile4.getText() != "...") {
            file = new File(this.lbFile4.getToolTipText());
            fileAttach.add(file);
        }
        //Receivers
        ArrayList<String> toReceiver = new ArrayList<String>();
        ArrayList<String> ccReceiver = new ArrayList<String>();
        ArrayList<String> bccReceiver = new ArrayList<String>();

        for (int i = 0; i < this.tableReceiver.getRowCount(); i++) {
            if (this.tableReceiver.getValueAt(i, 0) == "To:") {
                String addressToReceiver = this.tableReceiver.getValueAt(i, 1).toString();
                toReceiver = StringFunctions.cutStringByChac(addressToReceiver, ",");
            } else if (this.tableReceiver.getModel().getValueAt(i, 0) == "Cc:") {
                String addressCcReceiver = this.tableReceiver.getValueAt(i, 1).toString();
                ccReceiver = StringFunctions.cutStringByChac(addressCcReceiver, ",");
            } else if (this.tableReceiver.getModel().getValueAt(i, 0) == "Bcc:") {
                String addressBccReceiver = this.tableReceiver.getValueAt(i, 1).toString();
                bccReceiver = StringFunctions.cutStringByChac(addressBccReceiver, ",");
            }
        }
        //call function to sendmail
        MailStructureDTO mail = new MailStructureDTO();
        mail.setSender(sender);
        mail.setPass(pass);
        mail.setTitle(subject);

        Date date = new Date();
        mail.setDateSend(date);
        
        mail.setContent(this.txtAreaContent.getText().toString());
        mail.setAttachFiles(fileAttach);
        try {
            MailFunctionBUS.sendMail(mailServer, toReceiver, ccReceiver, bccReceiver, mail);
            try {
                //save message is sent
                MailFunctionBUS.saveMail(mail, ConstantValue.MailIn.Sent, ConstantValue.TypeMail.Personel);
            } catch (SQLException ex) {
                Logger.getLogger(FrmSendMailGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            JOptionPane.showMessageDialog(null, "Mail is sent successfully");
        } catch (IOException ex) {
            Logger.getLogger(FrmSendMailGUI.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_btSendActionPerformed

    private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveActionPerformed
        // TODO add your handling code here:
           String sender = this.cbSender.getSelectedItem().toString();
        String pass = "lethuangmail";   //temp
        String mailServer = "smtp.gmail.com";//temp

        int leftPos = sender.indexOf("<");
        int rightPos = sender.indexOf(">");
        sender = sender.substring(leftPos + 1, rightPos);

        //Subject
        String subject = this.tfSubject.getText();
        //File
        ArrayList<File> fileAttach = new ArrayList();
        File file;
        if (this.lbFile1.getText() != "...") {
            file = new File(this.lbFile1.getToolTipText());
            fileAttach.add(file);
        }
        if (this.lbFile2.getText() != "...") {
            file = new File(this.lbFile2.getToolTipText());
            fileAttach.add(file);
        }
        if (this.lbFile3.getText() != "...") {
            file = new File(this.lbFile3.getToolTipText());
            fileAttach.add(file);
        }
        if (this.lbFile4.getText() != "...") {
            file = new File(this.lbFile4.getToolTipText());
            fileAttach.add(file);
        }
//        //Receivers
//        ArrayList<String> toReceiver = new ArrayList<String>();
//        ArrayList<String> ccReceiver = new ArrayList<String>();
//        ArrayList<String> bccReceiver = new ArrayList<String>();
//
//        for (int i = 0; i < this.tableReceiver.getRowCount(); i++) {
//            if (this.tableReceiver.getValueAt(i, 0) == "To:") {
//                String addressToReceiver = this.tableReceiver.getValueAt(i, 1).toString();
//                toReceiver = StringFunctions.cutStringByChac(addressToReceiver, ",");
//            } else if (this.tableReceiver.getModel().getValueAt(i, 0) == "Cc:") {
//                String addressCcReceiver = this.tableReceiver.getValueAt(i, 1).toString();
//                ccReceiver = StringFunctions.cutStringByChac(addressCcReceiver, ",");
//            } else if (this.tableReceiver.getModel().getValueAt(i, 0) == "Bcc:") {
//                String addressBccReceiver = this.tableReceiver.getValueAt(i, 1).toString();
//                bccReceiver = StringFunctions.cutStringByChac(addressBccReceiver, ",");
//            }
//        }
        //call function to sendmail
        String error = "";
        MailStructureDTO mail = new MailStructureDTO();
        mail.setSender(sender);
        if(sender == null)
            error += "  Sender is not null";
        mail.setPass(pass);
        mail.setTitle(subject);
        if(subject == null)
            error += "  Subject is not null";
        Date date = new Date();
        mail.setDateSend(date);

        mail.setContent(this.txtAreaContent.getText().toString());
        if(mail.getContent() == null)
            error += "  Content is not null";
        mail.setAttachFiles(fileAttach);

        if(error.length() != 0)
        {
            JOptionPane.showMessageDialog(null, error);
            return;
        
        }
        try {
            MailFunctionBUS.saveMail(mail, ConstantValue.MailIn.Unsent, ConstantValue.TypeMail.Read);
        } catch (SQLException ex) {
            Logger.getLogger(FrmSendMailGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btSaveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrmSendMailGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAttach;
    private javax.swing.JButton btSave;
    private javax.swing.JButton btSend;
    private javax.swing.JComboBox cbSender;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lbFile1;
    private javax.swing.JLabel lbFile2;
    private javax.swing.JLabel lbFile3;
    private javax.swing.JLabel lbFile4;
    private javax.swing.JTable tableReceiver;
    private javax.swing.JTextField tfSubject;
    private javax.swing.JTextArea txtAreaContent;
    // End of variables declaration//GEN-END:variables
}
